﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Collections;
using System.Reflection;

namespace Zsea.Browser
{
    public static class Helper
    {
        // Methods
        //public static List<Cookie> GetAllCookie(this CookieContainer cc)
        //{
        //    List<Cookie> list = new List<Cookie>();
        //    Hashtable hashtable = (Hashtable)cc.GetType().InvokeMember("m_domainTable", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance, null, cc, new object[0]);
        //    foreach (object obj2 in hashtable.Values)
        //    {
        //        SortedList list2 = (SortedList)obj2.GetType().InvokeMember("m_list", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance, null, obj2, new object[0]);
        //        foreach (CookieCollection cookies in list2.Values)
        //        {
        //            foreach (Cookie cookie in cookies)
        //            {
        //                list.Add(cookie);
        //            }
        //        }
        //    }
        //    return list;
            
        //}

        public static IEnumerable<Cookie> All(this CookieContainer cc)
        {
            Hashtable hashtable = (Hashtable)cc.GetType().InvokeMember("m_domainTable", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance, null, cc, new object[0]);
            foreach (object obj2 in hashtable.Values)
            {
                SortedList list2 = (SortedList)obj2.GetType().InvokeMember("m_list", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance, null, obj2, new object[0]);
                foreach (CookieCollection cookies in list2.Values)
                {
                    foreach (Cookie cookie in cookies)
                    {
                        yield return cookie;
                    }
                }
            }
        }

        public static string GetCookieValue(this CookieContainer cc, string Name)
        {
            
            foreach (var cookie in cc.All())
            {
                if (cookie.Name.Equals(Name, StringComparison.CurrentCultureIgnoreCase))
                    return cookie.Name;
            }

            return null;
        }
    }


}
